<?php

namespace App\Models\Api;
//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 后台用户表
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;

class Goods extends Model
{

    protected $table = 'goods';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U';// 时间戳
    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    /**
     * 获取单个信息
     * @param $id
     */
    public static function getInfoById($id)
    {
        $info = self::where(['id' => $id])->select(['goods_name', 'goods_img', 'goods_price'])->first();
        if ($info) {
            return $info->toArray();
        }
        return [];
    }

    /**
     * 获取信息
     */
    public static function getAInfoById($id)
    {
        $info = self::where(['id' => $id])->first();
        if ($info) {
            $info = $info->toArray();
            $info['cate_name'] = GoodsCategory::getNameById($info['cate_id']);
            $info['goods_id'] = $id;
            $info['buy_num'] = 1;
            $info['get_num'] = 0;
            $info['sale_price'] = $info['goods_price'];
            $info['goods_img_list'] = json_decode($info['goods_img_list'], true);
            return $info;
        }
        return [];
    }

    /**
     * 获取数据集合
     * @param array $ids
     * @return mixed
     */
    public static function getListByIds($ids = [])
    {
        $info = self::select(['id', 'goods_price', 'goods_name', 'goods_img', 'goods_price as sale_price'])->whereIn('id', $ids)->get()->toArray();
        return $info;
    }
}
